<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <ButtonGroup>
          <a-button type="primary" @click="handleCreate" v-if="checkAuth('ADD-ROLE')">
            + 新增角色
          </a-button>

          <BasicImport
            @success="handleImport"
            :api="uploadRoleApi"
            :tempApi="tempRoleApi"
            v-if="checkAuth('EXPORT-ROLE')"
          >
            <a-button> 导入 </a-button>
          </BasicImport>

          <a-button @click="handleExport" v-if="checkAuth('CHECKOUT-ROLE')"> 导出 </a-button>
        </ButtonGroup>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                ifShow: checkAuth('EDIT-ROLE'),
                // icon: 'clarity:note-edit-line',
                label: '编辑',
                onClick: handleEdit.bind(null, record),
              },
              {
                ifShow: checkAuth('MENU-ROLE-AUTH'),
                // icon: 'ant-design:paper-clip-outlined',
                label: '菜单权限',
                onClick: handleAuth.bind(null, { ...record, hanldeType: 0 }),
              },
              {
                ifShow: checkAuth('ROLE-DELETE'),
                // icon: 'ant-design:delete-outlined',
                label: '删除',
                // color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
            :dropDownActions="[
              {
                ifShow: checkAuth('APP-MENU-ROLE-AUTH'),
                // icon: 'ant-design:paper-clip-outlined',
                label: 'APP菜单权限',
                onClick: handleAuth.bind(null, { ...record, hanldeType: 1 }),
              },
              {
                ifShow: checkAuth('DATA-ROLE-AUTH'),
                // icon: 'ant-design:paper-clip-outlined',
                label: '数据权限',
                onClick: handleUser.bind(null, record),
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <RoleModal @register="registerModal" @success="handleSuccess" />
    <RoleAuthModal @register="registerAuthModal" @success="handleSuccess" />
    <RoleUserModal @register="registerUserModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { Button } from 'ant-design-vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { BasicImport } from '/@/components/ImportButton';
  import RoleModal from './RoleModal.vue';
  import RoleAuthModal from './RoleAuthModal.vue';
  import RoleUserModal from './RoleUserModal.vue';

  import { downloadByData } from '/@/utils/file/download';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useModal } from '/@/components/Modal';

  import { setTableEmptyHeight } from '/@/utils';

  import { getBtnAuth, checkAuth } from '/@/utils/cutomPageBtnAuth/index';
  import { useRoute } from 'vue-router';

  import {
    getRoleListByPage,
    deleteRoleApi,
    exportRoleApi,
    tempRoleApi,
    uploadRoleApi,
  } from '/@/api/admin/role';
  import { columns, searchFormSchema } from './role.data';

  const ButtonGroup = Button.Group;

  export default defineComponent({
    name: 'SystemRole',
    components: {
      ButtonGroup,
      BasicTable,
      RoleModal,
      RoleAuthModal,
      RoleUserModal,
      TableAction,
      BasicImport,
    },
    setup() {
      const route = useRoute();

      const [registerModal, { openModal }] = useModal();
      const [registerAuthModal, { openModal: openModalAuth }] = useModal();
      const [registerUserModal, { openModal: openModalAuth2 }] = useModal();

      const { createMessage } = useMessage();

      const [registerTable, { reload }] = useTable({
        api: getRoleListByPage,
        columns,
        // descs
        beforeFetch(param) {
          return {
            ...param,
            descs: 'create_time',
          };
        },
        formConfig: {
          labelWidth: 75,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        immediate: false,
        // showIndexColumn: false,
        actionColumn: {
          width: 220,
          title: '操作',
          dataIndex: 'action',
          // slots: { customRender: 'action' },
          fixed: 'right',
        },
        indexColumnProps: {
          width: 100,
          align: 'left',
        },
        afterFetch(res) {
          setTableEmptyHeight(res);
        },
      });
      getBtnAuth(route, reload);
      function handleCreate() {
        openModal(true, {
          isUpdate: false,
        });
      }

      function handleAuth(record: Recordable) {
        openModalAuth(true, {
          record,
          isUpdate: true,
        });
      }
      function handleUser(record: Recordable) {
        openModalAuth2(true, {
          record,
          isUpdate: true,
        });
      }

      function handleEdit(record: Recordable) {
        openModal(true, {
          record,
          isUpdate: true,
        });
      }

      async function handleDelete(record: Recordable) {
        await deleteRoleApi(record.roleId);
        createMessage.success('操作成功');
        reload();
      }

      function handleSuccess() {
        getBtnAuth(route, reload);
      }
      function handleImport() {
        createMessage.success('操作成功');
        reload();
      }
      async function handleExport() {
        const file = await exportRoleApi();
        downloadByData(file, `role_${Date.now()}.xlsx`);
      }

      return {
        registerTable,
        registerModal,
        registerAuthModal,
        registerUserModal,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        handleImport,
        handleExport,
        handleAuth,
        handleUser,
        tempRoleApi,
        uploadRoleApi,
        checkAuth,
      };
    },
  });
</script>

<style scoped lang="less">
  :deep(.vben-basic-table .ant-table-wrapper) {
    padding-left: 16px;
    padding-top: 16px;
  }

  :deep(.vben-basic-table .ant-table-wrapper .ant-table-title) {
    padding-bottom: 16px !important;
  }

  :deep(.ant-btn-group) {
    .ant-btn {
      margin-right: 16px;
      border-radius: 4px;
    }
  }
</style>
